package kgame5.k3d.comp {
import kgame5.k3d.core.obj3d.node3d.Node3D;
import kgame5.k3d.core.view3d.View3D;

import flash.display.Sprite;
import flash.events.Event;

/**
 * @author kk
 * 模型查看器，如果需要交互，可以继承这个，然后加事件即可
 * 
 * 注意子类要设置好摄像机距离
 */
public class Obj3DViewer extends Sprite{
	
	private var view3D:View3D;
	
	protected var 
		Nearest:int=64*5,
		Farest:int=64*20;
	
	//要展示的那个东西
	private var md_content:Node3D;
	
	public function Obj3DViewer(w:int,h:int){
		//T.p(this,w,h);
		
		//加上背景颜色
		//GP.fillRect(this.graphics, bgColor, 0, 0, w, h);
		//3d视图
		
		view3D=new View3D(w,h,true);
		
		this.addChild(view3D.getDO());
		
		//测试用的，显示坐标轴
		//world.addChild(Factory3D.CreateAxis());
		
	}
	
	//设置要查看的内容
	public function setContent(md:Node3D){
		if(md_content!=null){
			md_content.dispose();
		}
		md_content=md;
		view3D.world.addChild(md_content);
		
		//摄像机重置
		view3D.camera.y=view3D.camera.radius=Nearest+(Farest-Nearest)*0.3;
		view3D.camera.pan=135;

		//更新一下
		this.update();
	}
	
	//清除
	public function clearContent(){
		if(md_content!=null){
			md_content.dispose();
		}
	}
	
	//执行单次更新
	public function update(){
		view3D.render();
	}
	
	public function startRender(){
		this.addEventListener(Event.ENTER_FRAME, efh);
	}
	public function stopRender(){
		this.removeEventListener(Event.ENTER_FRAME, efh);
	}
	
	private function efh(e:Event){
		update();
		//left(5);
	}
	
	//-------------摄像机的运动-----------------
	public function up(delta:int=10){
		view3D.camera.v3d_eye.y+=delta;
	}

	public function down(delta:int=10){
		view3D.camera.v3d_eye.y-=delta;
	}
	public function left(delta:int=5){
		view3D.camera.pan+=delta;
		
	}

	public function right(delta:int=5){
		view3D.camera.pan-=delta;
		
	}
	public function near(delta:int=10){
		view3D.camera.radius-=delta;
		if(view3D.camera.radius<Nearest){
			view3D.camera.radius=Nearest;
		}
	}

	public function far(delta:int=10){
		view3D.camera.radius+=delta;
		if(view3D.camera.radius>Farest){
			view3D.camera.radius=Farest;
		}
	}
	
}//end of class
}
